#include "BlogDetailsPage.h"

BlogDetailsPage::BlogDetailsPage(QWidget *parent)
    : QWidget{parent}
{
    setupUI();
    setupUIButtonStyle();
    connectSignals();
}

void BlogDetailsPage::setupUI()
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    reportReasonLabel = new QLabel(this);
    reportReasonLabel->setWordWrap(true); // 启用自动换行
    reportReasonLabel->setStyleSheet("padding: 8px; background-color: #f9f9f9; border: 1px solid #ddd; border-radius: 6px;");
    mainLayout->addWidget(reportReasonLabel);
    reportReasonLabel->setText("举报理由：文章举报演示");

    // 设置标题标签
    QHBoxLayout *topInfoLayout = new QHBoxLayout();
    titleLabel = new QLabel("QT 连接 MySQL8 提示 QMYSQL driver not loaded.txt", this);
    titleLabel->setStyleSheet("font-size: 24px; font-weight: bold;");
    reviewButton = new QPushButton("📝 审批", this);
    QString buttonStyle = R"(
        QPushButton {
            background-color: #f0f0f0;
            border: 1px solid #ccc;
            border-radius: 15px;
            font-size: 16px;
            font-weight: bold;
            color: #333;
        }
        QPushButton:hover {
            background-color: #e0e0e0;
        }
        QPushButton:pressed {
            background-color: #d0d0d0;
        }
    )";
    reviewButton->setFixedSize(110, 30);
    reviewButton->setStyleSheet(buttonStyle);
    topInfoLayout->addWidget(titleLabel);
    topInfoLayout->addStretch();
    topInfoLayout->addWidget(reviewButton);

    mainLayout->addLayout(topInfoLayout);

    contentBrowser = new QTextBrowser(this);
    contentBrowser->setStyleSheet(R"(
        QTextBrowser {
            border: 1px solid #ccc;
            border-radius: 8px;
            padding: 10px;
            background-color: #ffffff;
        }
    )");
    mainLayout->addWidget(contentBrowser);

    downloadButton = new QPushButton("附件下载", this);
    mainLayout->addWidget(downloadButton);

    setDownloadButtonVisible(true);

    QString exampleContent = "";
    QFile file("QT 连接 MySQL8 提示 QMYSQL driver not loaded.txt");
    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream in(&file);
        exampleContent = in.readAll(); // 读取全部内容
        file.close();
    }

    contentBrowser->setHtml(exampleContent);
}

void BlogDetailsPage::connectSignals()
{
    connect(reviewButton, &QPushButton::clicked, this, &BlogDetailsPage::onReviewClicked);
    connect(downloadButton, &QPushButton::clicked, this, &BlogDetailsPage::onDownloadClicked);
}

void BlogDetailsPage::onDownloadClicked()
{
    // 用户点击了确定，弹出文件夹选择对话框
    QString folderPath = QFileDialog::getExistingDirectory(this, "选择下载位置", QString());

    if (!folderPath.isEmpty())
    {
        // 假设附件名称为 attachment.zip
        QString fileName = "attachment.zip";
        QString filePath = folderPath + "/" + fileName;

        // 模拟下载：创建一个空文件作为示例
        QFile file(filePath);
        if (file.open(QIODevice::WriteOnly))
        {
            file.close();
            QMessageBox::information(this, "下载成功", "附件已保存至指定文件夹！");
        }
        else
        {
            QMessageBox::critical(this, "错误", "无法保存文件到指定目录！");
        }
    }
}

void BlogDetailsPage::setTitle(const QString &title)
{
    titleLabel->setText(title);
}

void BlogDetailsPage::setContent(const QString &htmlContent)
{
    contentBrowser->setHtml(htmlContent);
}

void BlogDetailsPage::setupUIButtonStyle()
{
    // 统一按钮样式
    QString buttonStyle = "QPushButton {"
                          "border: 1px solid #ccc;"
                          "border-radius: 8px;"
                          "padding: 6px 12px;"
                          "font-size: 14px;"
                          "background-color: #f5f5f5;"
                          "}"
                          "QPushButton:hover {"
                          "background-color: #e0e0e0;"
                          "}";
    downloadButton->setStyleSheet(buttonStyle);
}

void BlogDetailsPage::setDownloadButtonVisible(bool isVisible)
{
    downloadButton->setVisible(isVisible);
}

void BlogDetailsPage::setReportReason(const QString &reason)
{
    reportReasonLabel->setText(reason);
}

void BlogDetailsPage::onReviewClicked()
{
    BlogReviewDialog dialog(this);
    if (dialog.exec() == QDialog::Accepted)
    {
        QString comment = dialog.getApprovalResult();
        if (!comment.isEmpty())
        {
            // TODO: 提交到服务器或本地处理
            qDebug() << "处理结果" << comment;
        }
    }
}